package de.uv.kilter.core.implementation;

import de.uv.kilter.core.IConnection;
import de.uv.kilter.core.INode;


public class Connection implements IConnection {

	private int cost = 0;
	private int decreasedCost = 0;
	private int maximumCapacity = 0;
	private int minimumCapacity = 0;
	private int load = 0;
	
	private int state = 0;
	
	private long id = 0;
	private String name = null;
	
	private INode sourceNode = null;
	private INode targetNode = null;
	
	public Connection(){
		
	}
	
	public Connection(int cost, int minimumCapacity, int maximumCapacity,
			INode sourceNode, INode targetNode) {
		this.cost = cost;
		this.minimumCapacity = minimumCapacity;
		this.maximumCapacity = maximumCapacity;
		this.sourceNode = sourceNode;
		this.targetNode = targetNode;
		
		sourceNode.addOutboundingConnection(this);
		targetNode.addInboundingConnection(this);
//		setName("Connection: " + sourceNode.getName() + " ----> " + targetNode.getName());
	}

	@Override
	public int getCost() {
		return this.cost;
	}

	@Override
	public int getMaximumCapacity() {
		return this.maximumCapacity;
	}

	@Override
	public int getMinimumCapacity() {
		return this.minimumCapacity;
	}

	@Override
	public String getName() {
		return this.name;
	}

	@Override
	public INode getSourceNode() {
		return this.sourceNode;
	}

	@Override
	public INode getTargetNode() {
		return this.targetNode;
	}

	@Override
	public void setCost(int cost) {
		this.cost = cost;
		
	}

	@Override
	public void setMaximumCapacity(int maximumCapacity) {
		this.maximumCapacity = maximumCapacity;
		
	}

	@Override
	public void setMinimumCapacity(int minimumCapacity) {
		this.minimumCapacity = minimumCapacity;
	}

	@Override
	public void setName(String name) {
		this.name = name;
		
	}

	@Override
	public void setSourceNode(INode sourceNode) {
		if(this.sourceNode != null) {
			this.sourceNode.removeOutboundingConnection(this);
		}
		this.sourceNode = sourceNode;
		this.sourceNode.addOutboundingConnection(this);
	}

	@Override
	public void setTargetNode(INode targetNode) {
		if (this.targetNode != null) {
			this.targetNode.removeInboundingConnection(this);
		}
		this.targetNode = targetNode;
		this.targetNode.addInboundingConnection(this);
	}

	@Override
	public int getLoad() {
		return this.load;
	}

	@Override
	public void setLoad(int load) {
		this.load = load;
	}

	@Override
	public void addLoad(int load) {
		this.load += load;
	}

	@Override
	public long getId() {
		return this.id;
	}

	@Override
	public void setId(long id) {
		this.id = id;
	}
	
	public int getDecreasedCost() {
		return decreasedCost;
	}

	public void setDecreasedCost(int decreasedCost) {
		this.decreasedCost = decreasedCost;
	}

	public int getState() {
		return state;
	}

	public void setState(int state) {
		this.state = state;
	}

	/**
	 * @see de.uv.kilter.core.IConnection#calculateState()
	 */
	public void calculateState() {
		
		if (decreasedCost > 0)
		{
			if (load < minimumCapacity)
				state = -1;
			else if (load > minimumCapacity)
				state = -2;
			else
				state = +1;
		}
		else if (decreasedCost < 0)
		{
			if (load < maximumCapacity)
				state = -3;
			else if (load > maximumCapacity)
				state = -4;
			else
				state = +2;			
		}
		else
		{
			if (load < minimumCapacity)
				state = -5;
			else if (load > maximumCapacity)
				state = -6;
			else if ((minimumCapacity <= load) && (load <= maximumCapacity))
				state = +3;
			else 
				state = -100;
		}
	}
	
	/**
	 * @see de.uv.kilter.core.IConnection#calculateDecreasedCosts()
	 */
	public void calculateDecreasedCosts()
	{
		setDecreasedCost(getCost() + getSourceNode().getOriginalCapacity() - getTargetNode().getOriginalCapacity());
	}
	
	public String toString()
	{
		return getId() + ": " + sourceNode.getName() + " (oC: "+sourceNode.getOriginalCapacity()+" - "+ sourceNode.getCapacity()+") --> " +
			   targetNode.getName() + " (oC: "+targetNode.getOriginalCapacity()+" - "+ targetNode.getCapacity()+")  : state ("+state+") load ("+ load+") cost ("+cost+") decreasedCost ("+
		decreasedCost+") minCapacity ("+minimumCapacity+") maxCapacity ("+maximumCapacity+")";
	}
}
